package com.sk.cardeal.act.manual;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.dyhdyh.widget.loading.dialog.LoadingDialog;
import com.sk.cardeal.R;
import com.sk.cardeal.adapter.MaintainAdapter;
import com.sk.cardeal.base.BaseActivity;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.entity.MaintainBean;
import com.sk.cardeal.entity.Model;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.HttpAddress;
import com.sk.cardeal.utils.MyUtils;
import com.sk.cardeal.view.TitleView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by sk on 2018/3/8.
 */

public class MaintainActivity extends BaseActivity implements HandleDataCallBack {
    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.icon)
    ImageView icon;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;

    private String carId = "";
    private LoadingDialog loadingDialog;
    private Model model;
    private String url = "";

    @Override
    protected int getContentView() {
        return R.layout.activity_maintain;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        titleView.setMyCallBack(this);

        loadingDialog = MyUtils.getLoadingDialog(mContext, false);
        model = getIntent().getParcelableExtra("model");
        url = getIntent().getStringExtra("url");

        Glide.with(mContext).load(HttpAddress.ossPath+url).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(icon);
        tvName.setText(model.getText());

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));

        loadingDialog.show();
        AppApi.getMaintainInfo(model.getId(), this, 0, this);

    }

    @Override
    public void handlerSuccessData(String result, int tag) {
        loadingDialog.cancelDialog();
        MaintainBean maintainBean = JSON.parseObject(result,MaintainBean.class);
        List<MaintainBean.ResultListBean> dataBeanList = maintainBean.getResultList();
        MaintainAdapter maintainAdapter = new MaintainAdapter(R.layout.item_maintain,dataBeanList);
        View view = LayoutInflater.from(mContext).inflate(R.layout.empty_view,null);
        TextView textView = view.findViewById(R.id.content);
        textView.setText("暂无保养信息！");
        maintainAdapter.setEmptyView(view);
        dataRyc.setAdapter(maintainAdapter);

    }

    @Override
    public void handlerFailData(String failString, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void handleErrorData(String error, int tag) {
        loadingDialog.cancelDialog();
    }

    @Override
    public void clickTitle(int i) {
        super.clickTitle(i);
        switch (i){
            case 1:
                finish();
                break;
        }
    }
}
